package com.hotel.controller;

import com.hotel.entity.Checkin;
import com.hotel.service.CheckinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping ("/api")
public class CheckinController {
    @Autowired
    private CheckinService checkinService;
    //增
    @PostMapping("/admin/checkins")
    public void save(@RequestBody Checkin checkin) {
        checkinService.save(checkin);
    }
    //查
    @GetMapping("/admin/checkins")
    public List<Map<String,Object>> findAll(@RequestParam(required = false) Map<String,Object> map) {
        if(map.get("guest_name")!=null||map.get("endDate")!=null||map.get("room_id")!=null||map.get("id_card")!=null||map.get("startDate")!=null||map.get("status")!=null) {
            return checkinService.findByParam(map);
        }
        return checkinService.findAll();
    }
    //查可用房间
    @GetMapping("/admin/checkins/active")
    public List<Map<String,Object>> getActive() {
        return checkinService.getActive();
    }
    //按id查
    @GetMapping("/admin/checkins/{id}")
    public Optional<Map<String,Object>> findById(@PathVariable int id) {
        return checkinService.findById(id);
    }
    //退房
    @PutMapping("/admin/checkins/{id}/checkout")
    public void checkoutGuest(@PathVariable int id,@RequestBody Map<String,Object> params) {
        checkinService.checkout(id, params.get("check_out_time").toString());
    }
    //改
    @PostMapping("/admin/checkins/{id}")
    public void updateCheckin(@PathVariable int id, @RequestBody Checkin checkin) {
        checkinService.update(id, checkin);
    }
    //按身份证号查
    @GetMapping("/client/checkins/{id_card}")
    public List<Map<String, Object>> findCheckinsByIdCard(@PathVariable String id_card) {
        return checkinService.findCheckinsByIdCard(id_card);
    }
}